<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="card-title">
        <span>我的消息</span>
      </div>
      <div class="myMsg-box">
        <el-table
          :data="tableData"
          stripe
          style="width: 100%"
          :header-cell-style="{ background: '#e1e4e5' }"
        >
          <el-table-column prop="sendName" label="发送人" width="180">
          </el-table-column>
          <el-table-column prop="title" label="标题" width="180">
          </el-table-column>
          <el-table-column prop="sendTime" label="发送时间"> </el-table-column>
          <el-table-column prop="state" label="状态" :formatter="stateFormat">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="success"
                size="mini"
                @click="onCheck(scope.row)"
                plain
                >查看</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="onDelete(scope.row)"
                plain
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        @current-change="onPageChange"
        background
        layout="prev, pager, next"
        :page-count="pageData.totalPages"
        :current-page="pageData.curPage"
        :page-size="pageData.perPage"
      >
      </el-pagination>
      <el-dialog
        class="msg-dialog"
        :title="msgData.title"
        :visible.sync="dialogVisible"
        width="33%"
      >
        <el-card class="msgData-card">
          <div slot="header" class="clearfix">
            <span>发送人：</span>
            <span>{{ msgData.sendName }}</span>
          </div>
          <div>
            {{ msgData.content }}
          </div>
        </el-card>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      // 分页数据
      pageData: {
        // 总页数
        totalPages: 1,
        // 当前页
        curPage: 1,
        // 每页显示几条数据
        perPage: 3,
      },
      tableData: [],
      //   弹出框的隐藏控制
      dialogVisible: false,
      msgData: {
        title: "",
        sendName: "",
        content: "",
      },
    };
  },
  methods: {
    ...mapActions({
      getMsg: "message/getMsg", // 获取消息列表
      checkMsg: "message/checkMsg", // 更新消息已读状态
      delMsg: "message/delMsg", // 删除消息
    }),
    // 格式化已读状态
    stateFormat(row) {
      if (row.state === 0) {
        return "未读";
      } else {
        return "已读";
      }
    },
    // 获取消息列表
    getMessage() {
      this.getMsg(this.pageData).then((res) => {
        this.tableData = res.content;
        this.pageData.totalPages = res.paginateInfo.pages;
      });
    },
    // 当前页码发生变化时的回调
    onPageChange(index) {
      this.pageData.curPage = index;
      this.getMessage();
    },
    // 查看消息(更新已读状态)
    onCheck(row) {
      this.msgData.title = row.title;
      this.msgData.sendName = row.sendName;

      this.msgData.content = row.detail;
      this.dialogVisible = true;
      if (!row.state) {
        var msgId = {
          message_id: row.id,
        };
        this.checkMsg(msgId).then((res) => {
          row.state = 1;
        });
      }
    },
    onDelete(row) {
      var msgId = {
        message_id: row.id,
      };
      this.delMsg(msgId).then( res=>{
        this.$message({
          message: '删除消息成功',
          type: 'success'
        });
        this.getMessage()
      })
    },
  },
  created() {
    // 页面初始化完成获取消息列表
    this.getMessage();
  },
};
</script>

<style>
.msg-dialog .el-dialog__body {
  padding: 10px;
}
</style>

<style lang="scss" scoped>
.msgData-card {
  min-height: 200px;
}
</style>